package com.nvn.androidapp;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.nvn.androidapp.Objects.Document;
import com.nvn.androidapp.Objects.MenuDialog;
import com.nvn.androidapp.Objects.MenuDialog.OnOkClickListener;
import com.nvn.androidapp.adapters.DBAdapter;
import com.nvn.androidapp.adapters.ImageAdapter;
import com.nvn.androidapp.utils.Statics;

public class MainActivity extends FragmentActivity {

//	private boolean ready = false;
//	private CountDownTimer cDT;
	
	private GridView gallery;
	private ImageAdapter iAdapter;
	private ArrayList<Document> docs;
	private DBAdapter mDB;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		init();
	}


	private void init() {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		findViewById(R.id.imv_doc).setVisibility(View.VISIBLE);
		
//		cDT = new CountDownTimer(3000, 3000) {
//			
//			@Override
//			public void onTick(long arg0) {}
//			
//			@Override
//			public void onFinish() {
//				ready = false;
//			}
//		};
		
		mDB = new DBAdapter(this);
		docs = new ArrayList<Document>();
		
		
		iAdapter = new ImageAdapter(this, docs);
		
		gallery = (GridView)findViewById(R.id.gallery);
		gallery.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Intent i = new Intent(MainActivity.this, ViewPagerActivity.class);
				i.putExtra("DOC", docs.get(position));
				startActivity(i);
			}
		});
		
		WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
		
		gallery.setNumColumns(wm.getDefaultDisplay().getWidth() / Statics.PixelFromDp(this, Statics.SQUARE_MATRICS));
		
		gallery.setAdapter(iAdapter);
	}

	@Override
	protected void onResume() {
		mDB.getAllDocuments(docs);
		iAdapter.notifyDataSetChanged();
		super.onResume();
	}
	
	public void onDoc(View v){
		MenuDialog md = new MenuDialog(this);
		md.setOnOkClickListener(new OnOkClickListener() {
			
			@Override
			public void onOkClick(String txt) {
				Document doc = new Document();
				doc.setID(-1);
				doc.setType(txt);
				Intent i = new Intent(MainActivity.this, ViewPagerActivity.class);
				i.putExtra("DOC", doc);
				startActivity(i);
			}
		});
		md.show();
	}


	
	public static void showToast(Context ctx, String txt){
		Toast.makeText(ctx, txt, Toast.LENGTH_SHORT).show();
	}
	
//	@Override
//	public void onBackPressed() {
//		Fragment currentFragment=getSupportFragmentManager().findFragmentById(R.id.frame_content);
//		if(currentFragment instanceof GridViewFragment){
//			if(ready){
//				finish();
//			}else{
//				ready = true;
//				showToast(getBaseContext(), "Press back again to exit");
//				cDT.start();
//			}
//		}
//	}
	

	
}
